package com.example.hp.yuejian5.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.hp.yuejian5.R;
import com.example.hp.yuejian5.RetrofitListener.RetrofitListener;
import com.example.hp.yuejian5.activity.MySetActivity;
import com.example.hp.yuejian5.activity.My_DetailActivity;
import com.example.hp.yuejian5.bean.UserBean;
import com.example.hp.yuejian5.model.User_DetailModel;
import com.squareup.picasso.Picasso;

import static com.example.hp.yuejian5.other.url.USERPICTURE;

/**
 * A simple {@link Fragment} subclass.
 */
public class UserFragment extends Fragment {
    private String i_id, u_id;
    private Context context;
    private RetrofitListener<UserBean> listener;

    private ImageView user_detail_pic;
    private int flagattention;
    private SharedPreferences sp;
    private TextView user_detail_name;
    private Button my_change,my_detail;
    public UserFragment() {
        // Required empty public constructor
    }

    private void initView(View view) {
        user_detail_pic= view.findViewById(R.id.user_detail_pic);
        user_detail_name= view.findViewById(R.id.user_detail_name);
        my_change=view.findViewById(R.id.my_change);
        my_detail=view.findViewById(R.id.my_detail);
        my_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent (getActivity(),MySetActivity.class);
                intent.putExtra("m_id", i_id);
                getActivity().startActivity(intent);
            }
        });
        my_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent (getActivity(), My_DetailActivity.class);
                intent.putExtra("m_id", i_id);
                getActivity().startActivity(intent);
            }
        });

    }


    private void userDetail() {
        User_DetailModel user_detailModel = new User_DetailModel();
        user_detailModel.userDetail(u_id, listener);
    }




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Intent intent = getActivity().getIntent();
        i_id = (String) intent.getStringExtra("m_id");
        sp = getActivity().getSharedPreferences("user_id", Context.MODE_PRIVATE);
        u_id = sp.getString("userid", null);
        listener = new RetrofitListener<UserBean>() {
            @Override
            public void onSuccess(UserBean userBean, int flag) {
                user_detail_name.setText(userBean.getUser_name());

                Picasso.with(context).load(USERPICTURE + userBean.getUser_pic()).into(user_detail_pic);
            }

            @Override
            public void onFail() {

            }
        };

        View view= inflater.inflate(R.layout.fragment_user, container, false);

        initView(view);
        userDetail();

        return view;
    }

}
